package com.dingreading.cloud.ssxcx.service.impl;

import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.ssxcx.dto.CaCallNamesDto;
import com.dingreading.cloud.ssxcx.entity.CaCallNames;
import com.dingreading.cloud.ssxcx.entity.table.CaCallNamesTableDef;
import com.dingreading.cloud.ssxcx.mapper.CaCallNamesMapper;
import com.dingreading.cloud.ssxcx.service.CaCallNamesService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 课程点名表 服务层实现。
 *
 * @author EDY
 * @since 2025-08-21
 */
@Service
public class CaCallNamesServiceImpl extends BaseServiceImpl<CaCallNamesMapper, CaCallNames, CaCallNamesTableDef> implements CaCallNamesService {

    @Override
    protected CaCallNamesTableDef getTable() {
        return CaCallNamesTableDef.caCallNames;
    }

    @Override
    public Page<CaCallNamesDto> pageListSchedule(PageUtil pageUtil, String agencyUid, String storeUid, String studentUid, String projectUid) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.corpUid.eq(agencyUid))
                .and(table.storeUid.eq(storeUid))
                .and(table.memberUid.eq(studentUid));

        if (StringUtils.isNotBlank(projectUid))
            condition.and(table.projectUid.eq(projectUid));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());
        Page<CaCallNamesDto> caCallNamesDtoPage = getMapper().paginateWithRelationsAs(pageUtil.getPage(), pageUtil.getRows(), wrapper, CaCallNamesDto.class);
        if (CollectionUtils.isNotEmpty(caCallNamesDtoPage.getRecords())){
            for (CaCallNamesDto caCallNamesDto : caCallNamesDtoPage.getRecords()){
                caCallNamesDto.setProjectName(caCallNamesDto.getProductName());
            }
        }
        return caCallNamesDtoPage;
    }
}
